import Http from "../net/Http.js"


export default {
	async login() {
		try {
			const res = await login()
			const loginRes = await Http.post("/auth/wxlogin", {
				code: res.code
			})
			return loginRes
		} catch (e) {
			console.log(e)
		}
	},
	async getUserInfo() {
		return Http.get("/user/info")
	},
	async updateUserInfoByWx() {
		try {
			const userInfo = await getUserInfoByWx()
			const loginRes = await Http.post("/user/uploadPhoto", {
				cnname: userInfo.nickName,
				url: userInfo.avatarUrl,
			});
			return loginRes
		} catch (e) {
			console.log(e)
		}
	},

}

function login() {
	return new Promise((resolve, reject) => {
		uni.login({
			success(res) {
				resolve(res)
			},
			fail(err) {
				console.log(err)
				reject("默认登录失败")
			}
		})
	})
	return Http.get("banner")
}

function getUserInfoByWx() {
	return new Promise((resolve, reject) => {
		uni.getUserProfile({
			desc: "获取你的昵称、头像！",
			success(res) {
				// userInfo:
				// 	avatarUrl: ""
				// 	nickName: ""
				resolve(res.userInfo || {})
			},
			fail(err) {
				console.log(err)
				reject("获取微信用户信息失败失败")
			}
		})
	})
	return Http.get("banner")
}
